package com.java8.stream.bean;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author xuehu
 * @date 2020/12/1 10:37
 */

public class TestTransaction {

    List<Transaction> transactions = null;

    @Before
    public void before() {
        Trader raoul = new Trader("Raul", "Cambridge");
        Trader mario = new Trader("Mario", "Cambridge");
        Trader alan = new Trader("Alan", "BeiJing");
        Trader brian = new Trader("Brian", "ShangHai");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(alan, 2011, 400),
                new Transaction(mario, 2012, 710)
        );
    }

    //1.找出2011年发生的所有交易,并按交易额度排序(从低到高)
    @Test
    public void test1() {
        transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue()))
                .forEach(System.out::println);
    }


    //2.交易员都在哪些不同的城市工作过?
    @Test
    public void test2() {
        transactions.stream().distinct().forEach(transaction -> {
            System.out.println(transaction.getTrader().getCity());
        });
    }


    //3.查找所有来自剑桥的交易员,并按姓名排序
    @Test
    public void test3() {
        transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader).sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
                .forEach(System.out::println);
    }


    //4.返回所有交易员的姓名字符串,按字母顺序排序
    @Test
    public void tes4() {
        transactions.stream()
                .map((t) -> t.getTrader().getName())
                .sorted()
                .reduce("", String::concat);
    }
    //5.有没有交易员是在米兰工作的?
    //6.打印生活在剑桥的交易员的所有交易额.
    //7.所有交易中,最高的交易额是多少
    //8.找到交易额最小的交易
}
